home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / lh211src.zip / CRCIO_.ASM < prev    next >
Assembly Source File  |  1991-03-03  |  6KB  |  376 lines

  1. ;***********************************************
  2. ;    crcio_.asm -- input/output
  3. ;***********************************************
  4.             page    0, 128
  5.  
  6. include    amscls.inc
  7. $_init    GEN
  8.  
  9. CGROUP    GROUP    TEXT
  10. DGROUP    GROUP    DATA,BSS
  11.  
  12. TEXT    segment byte public 'CODE'
  13.     assume    cs:CGROUP, ds:DGROUP, ss:DGROUP
  14. TEXT    ends
  15.  
  16. DATA    segment byte public 'DATA'
  17. DATA    ends
  18.  
  19. BSS    segment byte public 'DATA'
  20.             public    infile_
  21. infile_        dw    1 dup (?)
  22.             public    outfile_
  23. outfile_    dw    1 dup (?)
  24.             public    dispflg_
  25. dispflg_    dw    1 dup (?)
  26.  
  27. crctable_    dw    100h dup (?)
  28.             public    crc_
  29. crc_        dw    1 dup (?)
  30.             public    bitbuf_
  31. bitbuf_        dw    1 dup (?)
  32.  
  33. subbitbuf_    db    1 dup (?)
  34. bitcount_    db    1 dup (?)
  35.  
  36.  
  37. BSS    ends
  38. TEXT    segment byte public 'CODE'
  39.     assume    cs:CGROUP, ds:DGROUP, ss:DGROUP
  40.  
  41. ;
  42. ;void make_crctable(uchar crcpoly)
  43. ;
  44.  
  45.     public    make_crctable_
  46. make_crctable_:
  47.     cld
  48.     push    ds
  49.     pop        es
  50.     push    cx
  51.     push    dx
  52.     push    di
  53.     mov        bx, ax
  54.     mov     di, offset DGROUP:crctable_
  55.     xor     dx, dx
  56.     $_do
  57.         mov     ax, dx
  58.         mov     cx, 8
  59.         $_do
  60.             $_if <shr ax, 1>, C
  61.                 xor     ax, bx
  62.             $_endif
  63.         $_until <LOOP>
  64.         stosw
  65.     $_until <inc dl>, Z
  66.     pop     di
  67.     pop     dx
  68.     pop     cx
  69.     ret
  70.  
  71. ;
  72. ;ushort calccrc(uchar *p, uint n)
  73. ;
  74.  
  75.     public    calccrc_
  76. calccrc_:        
  77.     push    cx
  78.     push    si
  79.     mov        si, ax
  80.     mov        cx, bx
  81.     mov        bx, crc_
  82.     jcxz    nofile
  83.     xor        ah, ah
  84.     cld
  85.     $_do
  86.         lodsb
  87.         xor        bl, al
  88.         mov        al, bh
  89.         mov        bh, ah
  90.         shl        bx, 1
  91.         mov        bx, crctable_[bx]
  92.         xor        bx, ax
  93.     $_until <LOOP>
  94. nofile:
  95.     mov        ax, bx
  96.     mov        crc_, ax
  97.     pop        si
  98.     pop        cx
  99.     ret
  100.  
  101. ;
  102. ;ushort getbits(uchar n)
  103. ;
  104.  
  105.     public    getbits_
  106. getbits_:        
  107.     push    cx
  108.     mov        cl, 16
  109.     sub        cl, al
  110.     push    bitbuf_
  111.     call    fillbuf_
  112.     pop        ax
  113.     shr        ax, cl
  114.     pop        cx
  115.     ret
  116.  
  117. ;
  118. ;    stream control structure of LSI C-86
  119. ;
  120.  
  121. iob        struc
  122. mode        db        ?
  123. ptr            dw        ?
  124. rcount        dw        ?
  125. wcount        dw        ?
  126. base        dw        ?
  127. bufsiz        dw        ?
  128. fd            dw        ?
  129. smallbuf    db        ?
  130. iob        ends
  131.  
  132. ;
  133. ;void fillbuf(uchar n)  /* Shift bitbuf n bits left, read n bits */
  134. ;
  135.  
  136.     public    fillbuf_
  137. fillbuf_:
  138.     push    cx
  139.     push    dx
  140.     mov        ch, al
  141.     mov        cl, bitcount_
  142.     mov        dx, bitbuf_
  143.     mov        al, subbitbuf_
  144.     $_if <cmp ch, cl>, A
  145.         sub        ch, cl
  146.         shl        dx, cl
  147.         rol        al, cl
  148.         add        dl, al
  149.         mov        cl, 8
  150. fb1:
  151.         xor        ax, ax
  152.         sub        word ptr compsize_, 1
  153.         sbb        word ptr compsize_ + 2, ax
  154.         $_if , NS
  155.             mov        bx, infile_
  156.             $_if <dec rcount[bx]>, NS
  157.                 inc        ptr[bx]
  158.                 mov        bx, ptr[bx]
  159.                 mov        al, [bx - 1]
  160.             $_else
  161.                 mov        ax, bx
  162.                 push    bx
  163.                 call    fgetc_
  164.                 pop        bx
  165.                 $_if <or ax, ax>, S
  166.                     mov        ax, offset DGROUP:RDERR_
  167.                     jmp        fileerror_
  168.                 $_endif
  169.             $_endif
  170.         $_endif
  171.         $_if <cmp ch, cl>, A
  172.             sub        ch, cl
  173.             mov        dh, dl
  174.             mov        dl, al
  175.             jmp        fb1
  176.         $_endif
  177.     $_endif
  178.     sub        cl, ch
  179.     mov        bitcount_, cl
  180.     mov        cl, ch
  181.     xor        ah, ah
  182.     shl        dx, cl
  183.     shl        ax, cl
  184.     add        dl, ah
  185.     mov        bitbuf_, dx
  186.     mov        subbitbuf_, al
  187.     pop        dx
  188.     pop        cx
  189.     ret
  190.  
  191.  
  192. ;
  193. ;void putbits(uchar n, ushort x)  /* Write rightmost n bits of x */
  194. ;
  195.  
  196.     public    putbits_
  197. putbits_:
  198.     push    cx
  199.     mov        cl, 16
  200.     sub        cl, al
  201.     shl        bx, cl
  202.     pop        cx
  203.  
  204.  
  205. ;
  206. ;void putcode(uchar n, ushort x)  /* Write rightmost n bits of x */
  207. ;
  208.     public    putcode_
  209. putcode_:
  210.  
  211.     push    cx
  212.     push    dx
  213.     mov        ch, al
  214.     mov        cl, bitcount_
  215.     mov        al, subbitbuf_
  216.     $_if <cmp ch, cl>, B
  217.         rol        bh, cl
  218.         or        al, bh
  219.         mov        subbitbuf_, al
  220.         sub        cl, ch
  221.         mov        bitcount_, cl
  222.         pop        dx
  223.         pop        cx
  224.         ret
  225.     $_endif
  226.     sub        ch, cl
  227.  
  228.     xor        dh, dh
  229.     mov        dl, bh
  230.     shl        dx, cl
  231.  
  232.     shl        bx, cl
  233.     or        al, dh
  234.     mov        dx, bx
  235.     mov        cl, 8
  236. pc1:
  237.     sub        word ptr compsize_, 1
  238.     sbb        word ptr compsize_ + 2, 0
  239.     jb        disable
  240.     mov        bx, outfile_
  241.     $_if <dec wcount[bx]>, NS
  242.         inc        ptr[bx]
  243.         mov        bx, ptr[bx]
  244.         mov        [bx - 1], al
  245.     $_else
  246.         xor        ah, ah
  247.         inc        wcount[bx]
  248.         call    fputc_
  249.         $_if <or ax, ax>, S
  250.             mov        ax, offset DGROUP:WTERR_
  251.             mov        bx, outfile_
  252.             jmp        fileerror_
  253.         $_endif
  254.     $_endif
  255.     $_if <cmp ch, cl>, AE
  256.         sub        ch, cl
  257.         mov        al, dh
  258.         mov        dh, dl
  259.         jmp        pc1
  260.     $_endif
  261.     mov        subbitbuf_, dh
  262.     sub        cl, ch
  263.     mov        bitcount_, cl
  264.     pop        dx
  265.     pop        cx
  266.     ret
  267.  
  268. disable:
  269.     mov        unpackable_, 1
  270.     inc        word ptr compsize_
  271.     inc        word ptr compsize_ + 2
  272.     pop        dx
  273.     pop        cx
  274.     ret
  275.  
  276.  
  277. ;
  278. ;int fread_crc(uchar *p, int n, FILE *f)
  279. ;
  280.  
  281.     public    fread_crc_
  282. fread_crc_:
  283.     push    cx
  284.     push    dx
  285.     mov        dx, ax
  286.     xchg    bx, cx
  287.     mov        bx, fd[bx]
  288.     mov        ah, 3fh
  289.     int        21h
  290.     mov        bx, ax
  291.     xchg    ax, dx
  292.     $_if <or bx, bx>, NZ
  293.         call    calccrc_
  294.     $_endif
  295.     mov        ax, dx
  296.     pop        dx
  297.     pop        cx
  298.     ret
  299.  
  300.  
  301. ;
  302. ;void fwrite_crc(uchar *p, int n, FILE *f)
  303. ;
  304.  
  305.     public    fwrite_crc_
  306. fwrite_crc_:
  307.     push    cx
  308.     push    dx
  309.     mov        dx, ax
  310.     push    bx
  311.     call    calccrc_
  312.     pop        bx
  313.     jcxz    fc0
  314.     xchg    bx, cx
  315.     push    bx
  316.     mov        bx, fd[bx]
  317.     mov        ah, 40h
  318.     int        21h
  319.     pop        dx
  320.     $_if <cmp ax, cx>, NE, AND
  321.         mov        ax, bx
  322.         call    isatty_
  323.     $_c  <or ax, ax>, Z
  324.         mov        ax, offset DGROUP:WTERR_
  325.         mov        bx, dx
  326.         jmp        fileerror_
  327.     $_endif
  328. fc0:
  329.     pop        dx
  330.     pop        cx
  331.     ret
  332.  
  333.  
  334. ;
  335. ;void init_getbits(void)
  336. ;
  337.  
  338.     public    init_getbits_
  339. init_getbits_:        
  340.     xor        ax, ax
  341.     mov        word ptr DGROUP:[bitbuf_], ax
  342.     mov        byte ptr DGROUP:[subbitbuf_], al
  343.     mov        byte ptr DGROUP:[bitcount_], al
  344.     mov        al,16
  345.     jmp        fillbuf_
  346.  
  347.  
  348. ;
  349. ;void init_putbits(void)
  350. ;
  351.  
  352.     public    init_putbits_
  353. init_putbits_:        
  354.     mov        byte ptr DGROUP:[bitcount_], 8
  355.     mov        byte ptr DGROUP:[subbitbuf_], 0
  356.     RET    
  357.  
  358.  
  359.     EXTRN    fileerror_:near
  360.     EXTRN    WTERR_:near
  361.     EXTRN    RDERR_:near
  362.     EXTRN    fgetc_:near
  363.     EXTRN    fwrite_:near
  364.     EXTRN    fread_:near
  365.     EXTRN    fputc_:near
  366.     EXTRN    isatty_:near
  367.     EXTRN    dispmark_:near
  368. TEXT    ends
  369.  
  370.     EXTRN    unpackable_:word
  371.     EXTRN    origsize_:dword
  372.     EXTRN    compsize_:dword
  373.     EXTRN    _iob_:byte
  374.  
  375.     END
  376.